﻿using CRMG.Container.Source.Variables;
using Guru.Framework.Util;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Windows.Forms;

namespace CRMG.Container.Views.BaseList
{
    public partial class FrmBaseList : Telerik.WinControls.UI.RadForm
    {
        protected DataTable dtSearch, dtMain;
        protected ResourceManager messageRes;
        protected ResourceManager formRes;
        protected int totalRows, totalPage, currentPage;
        private string searchTypeList = string.Empty;
        private string _columnHeaderList = string.Empty;
        protected static readonly int rowCount = AppVariables.RowCount;

        /// <summary>
        /// Base Constructor
        /// </summary>
        public FrmBaseList()
        {
            InitializeComponent();
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
            this.Width = Screen.PrimaryScreen.WorkingArea.Width;
            this.Height = Screen.PrimaryScreen.WorkingArea.Height;
            this.WindowState = FormWindowState.Maximized;
            SetEnableStateOfButtons();
            grdList.TableElement.RowHeaderColumnWidth = 0;
        }

        protected virtual void FormLoad(object sender, EventArgs e)
        {
        }


        protected void ReGenerateGrid()
        {
            try
            {
                totalRows = dtSearch.Rows.Count;
                totalPage = ComputeTotalPage(totalRows, rowCount);
                ReFillDropDown();
                currentPage = 0;
                SetPage(currentPage);
            }
            catch (Exception)
            {
                throw;
            }
        }


        protected void SetResources()
        {
            try
            {
                this.lblPage.Text = formRes.GetString("FrmBaseList.lblPage");
                this.btnGoToPage.Text = formRes.GetString("FrmBaseList.btnGoToPage");
                this.btnFirst.Text = formRes.GetString("FrmBaseList.btnFirst");
                this.btnPrevious.Text = formRes.GetString("FrmBaseList.btnPrevious");
                this.btnNext.Text = formRes.GetString("FrmBaseList.btnNext");
                this.btnLast.Text = formRes.GetString("FrmBaseList.btnLast");

                this.lblStudent.Text = formRes.GetString("FrmBaseList.lblStudent");
                this.lblUser.Text = formRes.GetString("FrmBaseList.lblUser");
                this.lblSearchType.Text = formRes.GetString("FrmBaseList.lblSearchType");
                this.btnSearch.Text = formRes.GetString("FrmBaseList.btnSearch");
                this.SearchTypeString = formRes.GetString("FrmBaseList.SearchTypeString");//"Sonrasında-Arasında-Öncesinde-Hepsi";               
            }
            catch (Exception)
            {
                throw;
            }
        }


        protected int ComputeTotalPage(int total_rows, int row_count)
        {
            row_count = row_count < 1 ? 1 : row_count;
            double d = (double)total_rows / row_count;
            d = Math.Floor(d);
            int retInt = (int)d;
            return retInt;
        }

        #region [ SetPage method ]

        /// <summary>
        /// Set page of Grid with given page number.
        /// </summary>
        /// <param name="page">page number</param>
        protected virtual void SetPage(int page)
        {
            try
            {
                grdList.DataSource = DataUtil.GetPageOfTable(dtSearch, page, rowCount);
                grdList.Refresh();
                SetEnableStateOfButtons();
                txtPage.Text = string.Format("{0} / {1}", currentPage + 1, totalPage + 1);
                grdList.Focus();
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ First,Previous Next, Last Button Click event. ]


        /// <summary>
        /// Occurs when First button clicked.
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">EventArgs object</param>
        protected virtual void First(object sender, EventArgs e)
        {
            return;
        }

        /// <summary>
        /// Occurs when Next button clicked.
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">EventArgs object</param>
        protected virtual void Next(object sender, EventArgs e)
        {
            return;
        }

        /// <summary>
        /// Occurs when Last button clicked.
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">EventArgs object</param>
        protected virtual void Last(object sender, EventArgs e)
        {
            return;
        }


        /// <summary>
        /// Occurs when Previous button clicked.
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">EventArgs object</param>
        protected virtual void Previous(object sender, EventArgs e)
        {
            return;
        }

        #endregion


        #region [ GoToPageClick method ]

        /// <summary>
        /// Occurs when Go To Page button clicked.
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">EventArgs object</param>
        protected virtual void GoToPageClick(object sender, EventArgs e)
        {
            try
            {
                int page = ConverterUtil.Convert2Int(drpPage.SelectedItem.Text);
                if (page > 0 && (page - 1) != currentPage)
                {
                    SetPage(page - 1);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ ReFillDropDown method ]

        /// <summary>
        /// Refills RadDropDownList with totalPage.
        /// </summary>
        protected void ReFillDropDown()
        {
            try
            {
                drpPage.Items.Clear();
                for (int i = 0; i < totalPage + 1; i++)
                {
                    drpPage.Items.Add((i + 1).ToString());
                }
                drpPage.SelectedIndex = totalPage > -1 ? 0 : -1;
            }
            catch (Exception)
            {
            }
        }

        #endregion


        #region [ SetEnableStateOfButtons ]

        /// <summary>
        /// Set Enable State of First, Previous, Next, Last buttons.
        /// </summary>
        protected void SetEnableStateOfButtons()
        {
            btnFirst.Enabled = totalPage != 0 && currentPage != 0;
            btnPrevious.Enabled = currentPage != 0;
            btnNext.Enabled = currentPage != totalPage;
            btnLast.Enabled = totalPage != 0 && currentPage != totalPage;
        }

        #endregion


        #region [ drpSearchTypeChange method ]

        /// <summary>
        /// SelectedIndexChanged Event method of drpsearchTypeChange.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drpSearchTypeChange(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (drpSearchType.Items.Count == 4)
            {
                lblT.Visible = false;
                dtpStart.Visible = false;
                dtpEnd.Visible = false;
                switch (drpSearchType.SelectedIndex)
                {
                    case 0:
                        dtpStart.Visible = true;
                        break;
                    case 1:
                        lblT.Visible = true;
                        dtpStart.Visible = true;
                        dtpEnd.Visible = true;
                        break;
                    case 2:
                        dtpEnd.Visible = true;
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion


        #region [ SearchTypeString Property ]

        /// <summary>
        /// Sets SearchType Drop Down items with '-' delimiter and Gets SearchTypeString.
        /// </summary>
        protected string SearchTypeString
        {
            get { return searchTypeList; }
            set
            {
                if (string.IsNullOrWhiteSpace(value) == false)
                {
                    searchTypeList = value;
                    drpSearchType.DataSource = searchTypeList.Split('-');
                    drpSearchType.SelectedIndex = drpSearchType.Items.Count - 1;
                }
            }
        }

        #endregion


        #region [ ColumnHeaderList Property ]

        /// <summary>
        /// Gets, Sets ColumnHeaderList of GridView.
        /// </summary>
        protected string ColumnHeaderList
        {
            get { return _columnHeaderList; }
            set
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(value) == false)
                    {
                        _columnHeaderList = value;
                        string[] colList = _columnHeaderList.Split('-');
                        if (colList != null)
                        {
                            int count = colList.Length > grdList.ColumnCount ? grdList.ColumnCount : colList.Length;
                            for (int i = 0; i < count; i++)
                            {
                                grdList.Columns[i].HeaderText = colList[i];
                                grdList.Columns[i].TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
                                grdList.Columns[i].HeaderTextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
                                if (colList[i].Contains("[i]"))
                                {
                                    grdList.Columns[i].IsVisible = false;
                                }

                            }
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        #endregion

        protected virtual void Search(object sender, EventArgs e)
        {
        }

        protected virtual void RefreshRecords(object sender, EventArgs e)
        {
        }

        protected virtual void grdListCellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
        }

    }
}
